/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "network.h"
#include "route.h"

static Network_JsonPointerType jsp;
static char valuebuf[64];

/* 小窗只有一个上传函数 */
uint8_t Route_xc_up(Route_PayloadType *rtpl)
{
  if((rtpl == NULL) || (rtpl->pl_len != sizeof(Xc_UpParaType)))
  {
    return 1;
  }
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  
  snprintf(valuebuf, 64, "%u", ((Xc_UpParaType*)(rtpl->payload))->curt_cod);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "xc_wm", jsp.js_value);
  
  snprintf(valuebuf, 64, "%u", ((Xc_UpParaType*)(rtpl->payload))->curt_pos);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "xc_khd", jsp.js_value);
  
  snprintf(valuebuf, 64, "%u", ((Xc_UpParaType*)(rtpl->payload))->out_time);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "xc_cs", jsp.js_value);
  
  snprintf(valuebuf, 64, "%u", ((Xc_UpParaType*)(rtpl->payload))->up_limit);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "xc_sxw", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID",  Route_str_pids[RT_DV_XC]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.xc[add2idx(rtpl->dev_addr)].did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}
